﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * 目标标本：4.0.30319.239
 * 
 * 最后修改：2011/12/9 23:52:02
 * ************************************************************/
namespace Mozlite.Common
{
    using System;
    using System.Linq;
    using System.Text;
    using System.Collections.Generic;
    using Mozlite.Common.Configuration;

    /// <summary>
    /// 导航管理类。
    /// </summary>
    public class Navigators
    {
        private Navigators() { }

        private static string GetCacheKey(Guid themeID) {
            return string.Format("Navigators::SettingsID::{0}::ThemeID::{1}", SiteSettings.Current.SettingsID, themeID);
        }

        /// <summary>
        /// 保存导航实例。
        /// </summary>
        /// <param name="navigator">导航实例对象。</param>
        /// <returns>返回保存结果。</returns>
        public static bool SaveNavigator(Navigator navigator)
        {
            if (CommonDataProvider.Current.SaveNavigator(navigator))
            {
                Caches.Remove(GetCacheKey(navigator.ThemeID));
                return true;
            }
            return false;
        }

        /// <summary>
        /// 加载导航列表。
        /// </summary>
        /// <param name="themeID">模板ID。</param>
        /// <returns>返回导航列表。</returns>
        public static Dictionary<NavigatorLocation, Dictionary<int, Navigator>> LoadNavigators(Guid themeID)
        {
            return Caches.Get<Dictionary<NavigatorLocation, Dictionary<int, Navigator>>>(GetCacheKey(themeID), Globals.MinuteSeconds * 3, Globals.MinuteSeconds * 3, () =>
            {
                return CommonDataProvider.Current.LoadNavigators(SiteSettings.Current.SettingsID, themeID);
            });
        }

        /// <summary>
        /// 获取当前位置的导航列表。
        /// </summary>
        /// <param name="themeID">模板ID。</param>
        /// <param name="location">导航位置。</param>
        /// <returns>返回导航位置列表。</returns>
        public static Dictionary<int, Navigator> GetNavigators(Guid themeID, NavigatorLocation location)
        {
            Dictionary<int, Navigator> navigators;
            if (LoadNavigators(themeID).TryGetValue(location, out navigators))
                return navigators;
            return new Dictionary<int, Navigator>();
        }

        /// <summary>
        /// 获取导航实例对象。
        /// </summary>
        /// <param name="navigatorID">导航ID。</param>
        /// <returns>返回导航实例对象。</returns>
        public static Navigator GetNavigator(int navigatorID)
        {
            return CommonDataProvider.Current.GetNavigator(SiteSettings.Current.SettingsID, navigatorID);
        }

        /// <summary>
        /// 获取子导航。
        /// </summary>
        /// <param name="navigator">当前导航。</param>
        /// <returns>返回导航列表。</returns>
        public static List<Navigator> GetChildren(Navigator navigator)
        {
            return (from nav in GetNavigators(navigator.ThemeID, navigator.Location).Values
                    where nav.ParentID == navigator.NavigatorID
                    select nav).ToList<Navigator>();
        }

        /// <summary>
        /// 获取父导航。
        /// </summary>
        /// <param name="navigator">当前导航。</param>
        /// <returns>返回父导航实例对象。</returns>
        public static Navigator GetParent(Navigator navigator)
        {
            Navigator nav;
            if (GetNavigators(navigator.ThemeID, navigator.Location).TryGetValue(navigator.ParentID, out nav))
                return nav;
            return null;
        }
    }
}